package com.pachong.service;

import com.pachong.dao.PaChongMapper;
import com.pachong.pojo.PaChong;
import com.pachong.unti.PureNetUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2018/4/9 0009.
 */
@Service
public class PaChongServiceImpl implements PaChongMapper{



@Resource
private PaChongMapper paChongMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return paChongMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(PaChong record) {
        return paChongMapper.insert(record);
    }

    @Override
    public int insertSelective(PaChong record) {
        return paChongMapper.insertSelective(record);
    }

    @Override
    public PaChong selectByPrimaryKey(Integer id) {
        return  paChongMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(PaChong record) {

        return paChongMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(PaChong record) {
        Integer pageCount =0;
        String url="http://www.zut.edu.cn";
        String urls="http://www.zut.edu.cn/index";
        String newsUrl="http://www.zut.edu.cn/";
        String flag="";
      // 获取首页的子网页的url
        Document doc = Jsoup.parse(PureNetUtil.get(url));
        Elements eles = doc.select(".bt em a");
        for(int i=0;i<eles.size();i++){
             if(i==0){
              String urlResult=eles.attr("href");
              System.out.println(urlResult+"-----------");
                flag=urlResult.split("/")[1].split(".htm")[0];
                newsUrl=newsUrl+urlResult;

            }else if(i==1){
                eles.attr("href");
            }else if(i==2){
                eles.attr("href");
            }
        }
         // 获取新闻子网页列表的每一个详情的url
        Document docNews = Jsoup.parse(PureNetUtil.get(newsUrl));
        System.out.println("新闻列表首页的url:"+newsUrl);
         // 找到页码
        Elements elesPage = docNews.select("#fanye67214");
        System.out.print("dsadjskdsa+"+elesPage.text());
       // 解析页码
        String page=elesPage.text().toString().split("共")[1];
        String pageCounts=page.split("条")[0];
        pageCount=Integer.valueOf(pageCounts);
        System.out.println("总条数："+pageCount);
        //计算页码
        if(pageCount%20==0){
            pageCount=pageCount/20;
        }else {
            pageCount=pageCount/20+1;
        }
        for(int i=pageCount;i>0;i--){
            if(i==pageCount){
                Elements elesNews = docNews.select(".winstyle67214 tbody td a");
                for (Element e : elesNews) {
                     String underUrl=e.attr("href");
                       underUrl=underUrl.replace("..",url);
//                   包含主网址才能进行操作
                    if(underUrl.contains(url)){
//                        标题
                        Document docNewsLook = Jsoup.parse(PureNetUtil.get(underUrl));
                        String lookNews = docNewsLook.select(".titlestyle67215 ").get(0).text();
                        record.setTitle(lookNews);
                        String times = docNewsLook.select(".timestyle67215").get(0).text();
                        record.setUsetime(times);
                        String user = docNewsLook.select(".authorstyle67215").text();
                        user=user.replace("&nbsp","");
                        record.setUsername(user);
                        System.out.println(underUrl);
                        String num=underUrl.split("/")[5].split(".htm")[0];
                        String scanNum = docNewsLook.select("#n"+num+"").text();
                        String  content = docNewsLook.getElementById("vsb_content_500").text();
                        record.setContent(content);
                        paChongMapper.updateByPrimaryKeySelective(record);

                    }
                   }
                }else {
                    String pageUrl=urls+"/"+flag+"/"+i+".htm";
                    System.out.println(pageUrl+"---");
                    Document docNewsPage = Jsoup.parse(PureNetUtil.get(pageUrl));
                    Elements  elesNewsPage = docNewsPage.select(".winstyle67214 tbody td a");
                   for (Element e : elesNewsPage) {
                    String underUrl=e.attr("href");
                    underUrl=underUrl.replace("..","");
//                   包含主网址才能进行操作
                    if(underUrl.contains(url)){
                        Document docNewsLook = Jsoup.parse(PureNetUtil.get(underUrl));
                        String lookNews = docNewsLook.select(".titlestyle67215 ").get(0).text();
                        record.setTitle(lookNews);
                        String times = docNewsLook.select(".timestyle67215").get(0).text();
                        record.setUsetime(times);
                        String user = docNewsLook.select(".authorstyle67215").text();
                        user=user.trim();
                        record.setUsername(user);
                        String resultLinks = docNewsLook.select(".authorstyle67215 > span").text();
                        String  content = docNewsLook.getElementById("vsb_content_500").text();
                        record.setContent(content);
                        paChongMapper.updateByPrimaryKeySelective(record);
                    }
                }
            }
        }
        return 0;
    }



    @Override
    public List<PaChong> selectByPaChong(PaChong record) {
        return paChongMapper.selectByPaChong(record);
    }

    @Override
    public List<PaChong> selectByPaChongPage(PaChong record) {
        return paChongMapper.selectByPaChongPage(record);
    }

    @Override
    public List<PaChong> selectByType(PaChong record) {
        return paChongMapper.selectByType(record);
    }
}
